package com.example.recordingpen_datacollection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.recordingpen_datacollection.entity.AccountAuthority;
import com.example.recordingpen_datacollection.entity.Identification;
import com.example.recordingpen_datacollection.entity.OfficialNotice;
import com.example.recordingpen_datacollection.entity.Query.OfficialNoticeQue;
import com.example.recordingpen_datacollection.entity.UserLogin;
import com.example.recordingpen_datacollection.entity.Vo.OfficialNoticeVo;
import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.service.AccountAuthorityService;
import com.example.recordingpen_datacollection.service.IdentificationService;
import com.example.recordingpen_datacollection.service.OfficialNoticeService;
import com.example.recordingpen_datacollection.service.UserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2024-06-28
 */
@Api(tags = "告示语 【admin】")
@RestController
@RequestMapping("/official-notice")
public class OfficialNoticeController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private IdentificationService identificationService;
    @Autowired
    private OfficialNoticeService officialNoticeService;

    @ApiOperation(value = "添加告示语 【admin】")
    @PostMapping("set")
    public RLIST setOfficialNotice(@RequestBody OfficialNoticeVo officialNoticeVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 56 ){
            return RLIST.error().message("无权操作");
        }
        Integer value = (Integer) redisService.getValue(token);
        boolean flat =  officialNoticeService.setOfficialNotice(officialNoticeVo,value);
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }


    @ApiOperation(value = "修改告示语 【admin】")
    @PostMapping("alter")
    public RLIST alterOfficialNotice(@RequestBody OfficialNoticeVo officialNoticeVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 56 ){
            return RLIST.error().message("无权操作");
        }
        Integer value = (Integer) redisService.getValue(token);
        boolean flat =  officialNoticeService.alterOfficialNotice(officialNoticeVo,value);
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }


    @ApiOperation(value = "查询自己告示语 【admin】")
    @PostMapping("get")
    public RLIST alterOfficialNotice(@RequestBody OfficialNoticeQue officialNoticeQue, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 56 ){
            return RLIST.error().message("无权操作");
        }
        if ((officialNoticeQue.getPageNo() == null || officialNoticeQue.getPageNo() <=0) ){
            officialNoticeQue.setPageNo(1);
        }
        if ((officialNoticeQue.getPageSize() == null || officialNoticeQue.getPageSize() <=0) ){
            officialNoticeQue.setPageSize(10);
        }
        Integer value = (Integer) redisService.getValue(token);
        RLIST rlist =  officialNoticeService.getOfficialNoticePage(officialNoticeQue,value);
        return rlist;
    }


    @ApiOperation(value = "操作告示语 【admin】")
    @PostMapping("handle")
    public RLIST handleOfficialNotice(@RequestBody OfficialNoticeVo officialNoticeVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 56 ){
            return RLIST.error().message("无权操作");
        }
        Integer value = (Integer) redisService.getValue(token);
        //数据校验
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",officialNoticeVo.getUid());

        Identification byId = identificationService.getById(officialNoticeVo.getUid());
        if (byId == null){
            return RLIST.error().message("标识不存在");
        }
        boolean flat =  officialNoticeService.handleOfficialNotice(byId.getId(),officialNoticeVo.getPid());
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }


}

